package com.example.day05rk.view

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.lib_base.entity.Res
import com.example.lib_base.entity.ViewEntity
import com.example.lib_base.entity.ViewEntityItem
import com.example.lib_base.net.ApiService

class ViewDataSource(val apiService: ApiService,val para:String,val type:String):PagingSource<Int,ViewEntityItem>() {
    override fun getRefreshKey(state: PagingState<Int, ViewEntityItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ViewEntityItem> {
        try {
//            val currentPage = params.key?:0
//            val view = apiService.getView()
//            val nextPage = if(view.data.size == 10)currentPage+1 else null
            var view : Res<ViewEntity>?= null
            if(type == "类型") {
                view = apiService.getView()
            }else if(type == "搜索") {
                view = apiService.searchName(para)
            }
            return LoadResult.Page(view?.data!!,null,null)
        }catch (e:Exception) {
            return LoadResult.Error(e)
        }
    }
}